package com.controller;

import com.alibaba.fastjson.JSON;
import com.mapper.EmployeeMapper;
import com.pojo.Empaccount;
import com.pojo.Employee;
import com.pojo.SystemMenu;
import com.service.EmpaccountService;
import com.service.EmployeeService;
import com.service.LoginService;
import com.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login")
@Transactional
public class LoginController {
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmpaccountService empaccountService;

//    菜单管理
    @RequestMapping("/menu")
    @ResponseBody
    public String menu(HttpSession session) {
        Integer empno= (Integer) session.getAttribute("loginEmpno");
        return JSON.toJSONString(sysMenuService.menu(empno));
    }

//    校验登录
    @RequestMapping("/checklogin")
    @ResponseBody
    public String checkLogin(HttpSession session,@RequestParam("empno") Integer empno, @RequestParam("password") String password){
        if (loginService.find(empno, password).size()==0){
        return "false";
     }else{
            session.setAttribute("loginEmpno",empno);
            String ename=employeeService.employeeFindByEmpno(empno).get(0).getEname();
            session.setAttribute("ename",ename);
         return "true";
     }
    }

    //        修改密码时验证本人信息
    @RequestMapping("/checkempinfo")
    @ResponseBody
    public String checkEmpinfo(@RequestParam("empno") Integer empno, @RequestParam("ename") String ename, @RequestParam("idcard") String idcard){
        List<Employee> employeelist=employeeService.findEmpnoEnameIdcard(empno, ename, idcard);
        if (employeelist.size()==0){
            return "false";
        }else {
            return "true";
        }
    }
//    修改密码
@RequestMapping("/updatepassword")
@ResponseBody
    public String updatepassword(@RequestParam("updempno") Integer updempno, @RequestParam("password")String password){
        int a=empaccountService.updateEmpaccount(updempno,password);
        if (a==0){
            return "false";
        }else {
            return "true";
        }

    }



}